package zzxkj.blog.service.Impl;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import zzxkj.blog.dto.FirstPageBlog;
import zzxkj.blog.search.model.SearchDocument;
import zzxkj.blog.search.mq.entity.EsMessage;
import zzxkj.blog.search.repository.BlogRepository;
import zzxkj.blog.service.BlogService;
import zzxkj.blog.service.SearchService;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    BlogRepository blogRepository;
    @Autowired
    BlogService blogService;
    @Autowired
    ModelMapper modelMapper;
    @Override
    public PageInfo search(PageInfo page, String keyword) {
        // 分页信息 mybatis plus的page 转成 jpa的page
        int current = page.getPageNum() - 1;
        int size = page.getPageSize();
        Pageable pageable = PageRequest.of(current,size);

        // 搜索es得到pageData
        MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(keyword,
                "title", "description");

        org.springframework.data.domain.Page<SearchDocument> docments = blogRepository.search(multiMatchQueryBuilder, pageable);

        // 结果信息 jpa的pageData转成mybatis plus的pageData
        PageInfo pageData = new PageInfo();
        pageData.setPageNum(page.getPageNum());
        pageData.setPageSize(page.getPageSize());
        pageData.setList(docments.getContent());
        pageData.setTotal(docments.getContent().size());
        return pageData;
    }

    @Override
    public long initEsData(List<FirstPageBlog> blogs) {
        if (blogs==null||blogs.size()==0) {
            return 0;
        }
        List<SearchDocument> documents = new ArrayList<>();
        for(FirstPageBlog firstPageBlog : blogs) {
            // 映射转换
            SearchDocument searchDocment = modelMapper.map(firstPageBlog, SearchDocument.class);
            searchDocment.setAvatar(firstPageBlog.getUser().getAvatar());
            searchDocment.setNickname(firstPageBlog.getUser().getNickname());
            documents.add(searchDocment);
        }
        blogRepository.saveAll(documents);
        return documents.size();
    }

    @Override
    public void createOrUpdateIndex(EsMessage message) {
      Long blogId = message.getBlogId();
      FirstPageBlog blog = blogService.getOneFirstBlog(blogId);
      SearchDocument searchDocument = modelMapper.map(blog,SearchDocument.class);
      searchDocument.setNickname(blog.getUser().getNickname());
      searchDocument.setAvatar(blog.getUser().getAvatar());
      blogRepository.save(searchDocument);
      log.info("es 的文档更新成功! -->{}",message.toString());
    }

    @Override
    public void removeIndex(EsMessage message) {
        Long blogId = message.getBlogId();
        blogRepository.deleteById(blogId);
        log.info("es 的文档删除成功！ ---> {}", message.toString());
    }
}
